{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-09-16T08:40:07.316027Z",
     "start_time": "2025-09-16T08:40:07.279784Z"
    }
   },
   "source": [
    "import asyncio\n",
    "import aiohttp\n",
    "import time\n",
    "\n",
    "async def download_one(url):\n",
    "    async with aiohttp.ClientSession() as session:\n",
    "        async with session.get(url) as resp:\n",
    "            print('Read {} from {}'.format(resp.content_length, url))\n",
    "\n",
    "async def download_all(sites):\n",
    "    tasks = [asyncio.create_task(download_one(site)) for site in sites]\n",
    "    await asyncio.gather(*tasks)\n",
    "\n",
    "def main():\n",
    "    sites = [\n",
    "        'https://en.wikipedia.org/wiki/Portal:Arts',\n",
    "        'https://en.wikipedia.org/wiki/Portal:History',\n",
    "        'https://en.wikipedia.org/wiki/Portal:Society',\n",
    "        'https://en.wikipedia.org/wiki/Portal:Biography',\n",
    "        'https://en.wikipedia.org/wiki/Portal:Mathematics',\n",
    "        'https://en.wikipedia.org/wiki/Portal:Technology',\n",
    "        'https://en.wikipedia.org/wiki/Portal:Geography',\n",
    "        'https://en.wikipedia.org/wiki/Portal:Science',\n",
    "        'https://en.wikipedia.org/wiki/Computer_science',\n",
    "        'https://en.wikipedia.org/wiki/Python_(programming_language)',\n",
    "        'https://en.wikipedia.org/wiki/Java_(programming_language)',\n",
    "        'https://en.wikipedia.org/wiki/PHP',\n",
    "        'https://en.wikipedia.org/wiki/Node.js',\n",
    "        'https://en.wikipedia.org/wiki/The_C_Programming_Language',\n",
    "        'https://en.wikipedia.org/wiki/Go_(programming_language)'\n",
    "    ]\n",
    "    start_time = time.perf_counter()\n",
    "    asyncio.run(download_all(sites))\n",
    "    end_time = time.perf_counter()\n",
    "    print('Download {} sites in {} seconds'.format(len(sites), end_time - start_time))\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    main()"
   ],
   "outputs": [
    {
     "ename": "RuntimeError",
     "evalue": "asyncio.run() cannot be called from a running event loop",
     "output_type": "error",
     "traceback": [
      "\u001B[31m---------------------------------------------------------------------------\u001B[39m",
      "\u001B[31mRuntimeError\u001B[39m                              Traceback (most recent call last)",
      "\u001B[36mCell\u001B[39m\u001B[36m \u001B[39m\u001B[32mIn[5]\u001B[39m\u001B[32m, line 38\u001B[39m\n\u001B[32m     35\u001B[39m     \u001B[38;5;28mprint\u001B[39m(\u001B[33m'\u001B[39m\u001B[33mDownload \u001B[39m\u001B[38;5;132;01m{}\u001B[39;00m\u001B[33m sites in \u001B[39m\u001B[38;5;132;01m{}\u001B[39;00m\u001B[33m seconds\u001B[39m\u001B[33m'\u001B[39m.format(\u001B[38;5;28mlen\u001B[39m(sites), end_time - start_time))\n\u001B[32m     37\u001B[39m \u001B[38;5;28;01mif\u001B[39;00m \u001B[34m__name__\u001B[39m == \u001B[33m'\u001B[39m\u001B[33m__main__\u001B[39m\u001B[33m'\u001B[39m:\n\u001B[32m---> \u001B[39m\u001B[32m38\u001B[39m     \u001B[43mmain\u001B[49m\u001B[43m(\u001B[49m\u001B[43m)\u001B[49m\n",
      "\u001B[36mCell\u001B[39m\u001B[36m \u001B[39m\u001B[32mIn[5]\u001B[39m\u001B[32m, line 33\u001B[39m, in \u001B[36mmain\u001B[39m\u001B[34m()\u001B[39m\n\u001B[32m     15\u001B[39m sites = [\n\u001B[32m     16\u001B[39m     \u001B[33m'\u001B[39m\u001B[33mhttps://en.wikipedia.org/wiki/Portal:Arts\u001B[39m\u001B[33m'\u001B[39m,\n\u001B[32m     17\u001B[39m     \u001B[33m'\u001B[39m\u001B[33mhttps://en.wikipedia.org/wiki/Portal:History\u001B[39m\u001B[33m'\u001B[39m,\n\u001B[32m   (...)\u001B[39m\u001B[32m     30\u001B[39m     \u001B[33m'\u001B[39m\u001B[33mhttps://en.wikipedia.org/wiki/Go_(programming_language)\u001B[39m\u001B[33m'\u001B[39m\n\u001B[32m     31\u001B[39m ]\n\u001B[32m     32\u001B[39m start_time = time.perf_counter()\n\u001B[32m---> \u001B[39m\u001B[32m33\u001B[39m \u001B[43masyncio\u001B[49m\u001B[43m.\u001B[49m\u001B[43mrun\u001B[49m\u001B[43m(\u001B[49m\u001B[43mdownload_all\u001B[49m\u001B[43m(\u001B[49m\u001B[43msites\u001B[49m\u001B[43m)\u001B[49m\u001B[43m)\u001B[49m\n\u001B[32m     34\u001B[39m end_time = time.perf_counter()\n\u001B[32m     35\u001B[39m \u001B[38;5;28mprint\u001B[39m(\u001B[33m'\u001B[39m\u001B[33mDownload \u001B[39m\u001B[38;5;132;01m{}\u001B[39;00m\u001B[33m sites in \u001B[39m\u001B[38;5;132;01m{}\u001B[39;00m\u001B[33m seconds\u001B[39m\u001B[33m'\u001B[39m.format(\u001B[38;5;28mlen\u001B[39m(sites), end_time - start_time))\n",
      "\u001B[36mFile \u001B[39m\u001B[32m~\\AppData\\Local\\Programs\\Python\\Python313\\Lib\\asyncio\\runners.py:191\u001B[39m, in \u001B[36mrun\u001B[39m\u001B[34m(main, debug, loop_factory)\u001B[39m\n\u001B[32m    161\u001B[39m \u001B[38;5;250m\u001B[39m\u001B[33;03m\"\"\"Execute the coroutine and return the result.\u001B[39;00m\n\u001B[32m    162\u001B[39m \n\u001B[32m    163\u001B[39m \u001B[33;03mThis function runs the passed coroutine, taking care of\u001B[39;00m\n\u001B[32m   (...)\u001B[39m\u001B[32m    187\u001B[39m \u001B[33;03m    asyncio.run(main())\u001B[39;00m\n\u001B[32m    188\u001B[39m \u001B[33;03m\"\"\"\u001B[39;00m\n\u001B[32m    189\u001B[39m \u001B[38;5;28;01mif\u001B[39;00m events._get_running_loop() \u001B[38;5;129;01mis\u001B[39;00m \u001B[38;5;129;01mnot\u001B[39;00m \u001B[38;5;28;01mNone\u001B[39;00m:\n\u001B[32m    190\u001B[39m     \u001B[38;5;66;03m# fail fast with short traceback\u001B[39;00m\n\u001B[32m--> \u001B[39m\u001B[32m191\u001B[39m     \u001B[38;5;28;01mraise\u001B[39;00m \u001B[38;5;167;01mRuntimeError\u001B[39;00m(\n\u001B[32m    192\u001B[39m         \u001B[33m\"\u001B[39m\u001B[33masyncio.run() cannot be called from a running event loop\u001B[39m\u001B[33m\"\u001B[39m)\n\u001B[32m    194\u001B[39m \u001B[38;5;28;01mwith\u001B[39;00m Runner(debug=debug, loop_factory=loop_factory) \u001B[38;5;28;01mas\u001B[39;00m runner:\n\u001B[32m    195\u001B[39m     \u001B[38;5;28;01mreturn\u001B[39;00m runner.run(main)\n",
      "\u001B[31mRuntimeError\u001B[39m: asyncio.run() cannot be called from a running event loop"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python313\\Lib\\site-packages\\pygments\\regexopt.py:26: RuntimeWarning: coroutine 'download_all' was never awaited\n",
      "  def regex_opt_inner(strings, open_paren):\n",
      "RuntimeWarning: Enable tracemalloc to get the object allocation traceback\n"
     ]
    }
   ],
   "execution_count": 5
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
